#include "Tools/RIVIAToolMgr.h"
#include "Tools/RIVIAToolBase.h"

RIVIAToolMgr* RIVIAToolMgr::m_pInstance = nullptr;
QMutex RIVIAToolMgr::m_lockMutex;

RIVIAToolMgr *RIVIAToolMgr::getInstance()
{
    if(nullptr == m_pInstance)
    {
        QMutexLocker oLocker(&m_lockMutex);
        {
            if(nullptr == m_pInstance)
            {
                m_pInstance = new RIVIAToolMgr();
            }
        }
    }
    return m_pInstance;
}

void RIVIAToolMgr::freeInstance()
{
    if (nullptr != m_pInstance)
    {
        QMutexLocker oLocker(&m_lockMutex);
        {
            if (nullptr != m_pInstance)
            {
                delete m_pInstance;
                m_pInstance = nullptr;
            }
        }
    }
}

RIVIAToolMgr::RIVIAToolMgr() :
    m_pToolsHash(nullptr),
    m_enCurTool(PencilTool)
{
    m_pToolsHash = new QHash<QString, RIVIAToolBase*>;
}

RIVIAToolMgr::~RIVIAToolMgr()
{
    QHash<QString, RIVIAToolBase*>::iterator Iter;
    for(Iter = m_pToolsHash->begin(); Iter != m_pToolsHash->end(); ++Iter)
    {
        delete Iter.value();
    }
    delete m_pToolsHash;
    m_pToolsHash = nullptr;
}

void RIVIAToolMgr::addTool(const QString &strToolName, RIVIAToolBase *pTool)
{
    m_pToolsHash->insert(strToolName, pTool);
}

RIVIAToolBase *RIVIAToolMgr::getTool(const QString &strToolName)
{
    return m_pToolsHash->value(strToolName, nullptr);
}

ToolState RIVIAToolMgr::getEnCurTool() const
{
    return m_enCurTool;
}

void RIVIAToolMgr::setEnCurTool(const ToolState &enCurTool)
{
    m_enCurTool = enCurTool;
}
